/**
 * Created by kevin on 16/8/16.
 */
var eosi = window.eosi || {};
this.eosi.fotile = this.eosi.fotile || {};
//this.eosi.fotile = this.eosi.fotile || {};
//this.eosi.fotile = function(name) {
//    this.name = name;
//};

(function (window) {
    // the application

    function FotileMaster(fotile) {
        this.version = fotile.version
        //alert(this.version)
        //alert("aa");
        //console.log(Fotile.isMute)
        //this.init(fotile);
        this.Container_constructor();

        this.init(fotile);

    }
    var p = createjs.extend(FotileMaster, createjs.Container);

    // class properties:

    FotileMaster.MAX_THRUST = 2;
    FotileMaster.MAX_VELOCITY = 5;

    // public properties:
    p.version = null;
    p.fotile = null;
    p.master_UILoader = null;
    p.animation_Loader = null;
    p.animation_is_loaded = false;//动画是否已经加载完毕
    p.container = null;
    p.sound_switcher = null;
    p.bg_sound_status = false;
    //p.loadProxy = null;
    //p.soundInstance =null;
    p.soundLoader = null;
    //p.isMute = true;
    p.firstVideo = null;//第一个视频的实例
    p.playFirstVideo = false;//是否播放第一个视频

    p.leftVideo = null;//第二个视频
    p.playLeftVideo = false;

    // public methods:
    p.init = function (fotile) {
        this.fotile = fotile;
        //console.log(this.fotile);
        //this.container = new createjs.Container();
        //this.fotile.container.addChild(this);
        console.log("I'm Master!!!");
        //this.addEventListener('added',onAdded);
        //this.loadMasterUIRes();
        this.loadMusic();


        //console.log(this)
    }

    /*p.loadMusic = function () {
        var master = this;
        console.log("加载主机音乐资源");
        // Create a single item to load.
        var assetsPath = "./_assets/static/";
        //var item = {src:assetsPath+"M-GameBG.ogg", id:"music"};
        var manifest = [
            {id: "music", src: assetsPath + "M-GameBG.ogg"}
        ]

        // Instantiate a queue.
        soundLoader = new createjs.LoadQueue();
        createjs.Sound.alternateExtensions = ["mp3"];	// add other extensions to try loading if the src file extension is not supported
        soundLoader.installPlugin(createjs.Sound);
        soundLoader.addEventListener("complete", loadComplete);
        soundLoader.addEventListener("fileload", fileComplete);
        soundLoader.addEventListener("error", handleFileError);
        soundLoader.addEventListener("fileprogress", handleFileProgress);
        soundLoader.addEventListener("progress", handleProgress);
        soundLoader.loadManifest(manifest);

        function handleFileError(evt) {
            console.log("error ", evt);
            // An error occurred.
        }
        function handleFileProgress(evt) {
            // Progress happened.
            console.log("Event File Loading: " + (soundLoader.progress.toFixed(2) * 100) + "%");
        }
        function handleProgress(evt) {
            console.log("Event Loading: " + (soundLoader.progress.toFixed(2) * 100) + "%");
            // Progress happened.
        }
        function loadComplete(evt) {
            // Load completed.
            console.log("sounds loaded Complete :)");
            //playSound("music");
        }
        function fileComplete(evt) {
            console.log("Event Callback file loaded ", evt);
        }
        function playSound(name) {
            // Play the sound using the ID created above.
            //createjs.Sound.muted = master.fotile.switchSound();
            createjs.Sound.muted = master.fotile.isMute;
            return createjs.Sound.play(name);
        }

    }*/

    p.tick = function  (event) {
        //console.log(event);
    }



    //加载主机初始化UI资源除了声音
    p.loadMasterUIRes = function (){

        console.log("加载主机初始化UI资源");
        var manifest = [
            //{src:"lib/tweenjs-0.6.1.min.js",id:"tweenjs"},
            //{src:"lib/soundjs-NEXT.min.js",id:"soundjs"},
            {src:"js/VideoMasterFirst.js?r="+this.version,id:"VideoMasterFirst"},
            {src:"js/Fotilewait.js?r="+this.version,id:"Fotilewait"},
            {src:"js/VideoPublicLeft.js",id:"VideoPublicLeft"},
            {src:"static/master/UI/bg.jpg",id:"bg"},
            {src:"static/master/UI/play.png",id:"bn_play"},
            //{src:"static/M-GameBG.ogg",id:"bg_music"},

        ];
        //console.log(this)
        var master = this;
        master_UILoader=null;
        master_UILoader = new createjs.LoadQueue(true);
        //createjs.Sound.registerPlugins([createjs.HTMLAudioPlugin]);  // need this so it doesn't default to Web Audio
        //master_UILoader.installPlugin(createjs.Sound);
        master_UILoader.on("fileload", masterFileLoaded);
        master_UILoader.on("error", masterUILoadError);
        master_UILoader.on("complete", masterUILoadComplete);
        master_UILoader.on("progress", updateLoading,true);
        //master_UILoader.addEventListener("progress", updateLoading,true);
        master_UILoader.loadManifest(manifest, true, "./_assets/");
        //加载完毕
        function masterUILoadComplete(event){
            //console.log(master)
            //console.log(master.fotile)
            //console.log(master)
            //master.loadMusic();
            clearLoading();
            master.setupMasterUI();
            //master.loadMasterAnimationRes();

            //createjs.Ticker.timingMode = createjs.Ticker.RAF;
            //createjs.Ticker.on("tick", tick, this);
        }
        function clearLoading(){
            console.log("cleanning master load info");
        }
        //文件加载失败
        function masterUILoadError(event) {
            console.log("主机UI文件加载失败："+event.target);
        }

        function masterFileLoaded(event){
            //console.log(this)
            var item = event.item;
            var id = item.id;
            var result = event.result;
            //console.log(result.src);

            switch (item.type) {

                case createjs.AbstractLoader.IMAGE:

                    break;

                case createjs.AbstractLoader.JAVASCRIPT:
                    break;

            }

        }

        //function updateLoading(event) {
            //console.log(event);
        //}
    }

    p.updateLoading =function(event) {
        console.log(event);
    }


    //master UI
    p.setupMasterUI = function (){

        // console.log(master_UILoader.getResult("bn_play"))

        // this.initFirstVideo();
        this.setinitwait();
        return false;
        

        //var firstVideo = this.firstVideo;
        //console.log(this.master)

        //alert("aa");
        //console.log(master_UILoader)
        /*var bg = new createjs.Bitmap(master_UILoader.getResult("bg"));
        //alert(bg.getBounds());
        //alert(getWidth());
        //var ratioX =
        bg.scaleX = this.fotile.getWidth()/bg.getBounds().width;
        bg.scaleY = this.fotile.getHeight()/bg.getBounds().height;
        //bg.setBounds = (0,0,1000,getHeight());
        bg.alpha = 0;*/

        var bn = new createjs.Bitmap(master_UILoader.getResult("bn_play"));
        //bn.shadow = new createjs.Shadow("#000000", 0, 0, 10);

        bn.addEventListener("click",clickPlayBn);
        function clickPlayBn(evt){
            //this = windows
            //console.log(this.master.firstVideo)

            //createjs.Tween.get(bn)
            //      .to({ alpha:0}, 200, createjs.Ease.sineOut);
            //alert(evt);
            //loadVideo();
            this.FotileMaster.playFirstVideo = true;
            if(this.FotileMaster.firstVideo.video_is_loaded && this.FotileMaster.music_is_loaded){
                this.FotileMaster.firstVideo.videoSprite.play();
                createjs.Sound.play("firstduibai");
                clearMasterUI();
                bn.removeEventListener("click", clickPlayBn);
            }else{
                console.log("loading is not done!!")
            }


            //master.initFirstVideo();
            /*if(animation_Loader.loaded){
                master.page1();
            }else{
                alert(animation_Loader.loaded);
            }*/
            //alert(animation_Loader.loaded);
        }
        bn.x = (this.fotile.getWidth()-bn.getBounds().width)/2;
        bn.y = this.fotile.getHeight()/2+150;
        bn.alpha = 0;

        // set up a tween that tweens between scale 0.3 and 1 every second.
        createjs.Tween.get(bn)
            //.wait(300)
            .to({ y: (this.fotile.getHeight()-bn.getBounds().height)/2,alpha:1}, 500, createjs.Ease.sineOut)

        //console.log(container);
        this.addChild(bn);
        //stage.update();

        //清除masterUI
        function clearMasterUI(){
            createjs.Tween.get(bn)
                //.wait(500)
                .to({ alpha:0}, 500, createjs.Ease.sineOut)
            //stage.removeChild(bg,bn);
            //createjs.Tween.get(bg)
            //    .wait(500)
            //    .to({alpha:0}, 500).call(bgOutComplete);
            function bgOutComplete() {
                //渐变完成执行
                //loadVideo();

            }
        }

    }

    //加载主机初始化UI资源
    p.loadMasterAnimationRes = function (){
        var master = this;
        var manifest = [
            {src:"static/master/animation/p1_pic_1.jpg",id:"p1_pic_1"},
            {src:"static/master/animation/p1_pic_2_1.jpg",id:"p1_pic_2_1"},
            {src:"static/master/animation/p1_pic_2_2.jpg",id:"p1_pic_2_2"},
            {src:"static/master/animation/p1_pic_3.jpg",id:"p1_pic_3"},
            {src:"static/master/animation/p1.jpg",id:"p1"},
            {src:"static/master/animation/popup_res.png",id:"pop"},

        ];

        animation_Loader = new createjs.LoadQueue(true);
        //animation_Loader.on("error", animationLoaderError);
        animation_Loader.on("complete", animationLoadComplete);
        animation_Loader.addEventListener("progress", updateLoading,true);
        animation_Loader.loadManifest(manifest, true, "./_assets/");
        //动画资源加载完毕
        function animationLoadComplete (evt){
            //setupAnimationUI();
            //console.log(master.animation_is_loaded)
            master.animation_is_loaded = true;
            //console.log(master.animation_is_loaded)
        };
    }



    //图片加边框
    p.drawPicBoder = function (w,h,boder_thick){
        var border = new createjs.Shape();
        border.graphics.setStrokeStyle(boder_thick);
        border.graphics.beginStroke("#000000");
        border.graphics.beginFill("#ad628a").drawRect(0,0, w, h);
        //border.graphics.beginFill("#000000").drawRect(-boder_thick, -boder_thick, w+boder_thick*2, h+boder_thick*2);
        return border;
    }


    //实例前面的视频
    p.initFirstVideo = function(){

        this.firstVideo = new eosi.fotile.VideoMasterFirst(this);
        this.addChild(this.firstVideo);
        //console.log(this.firstVideo);
        //this.firstVideo = new eosi.fotile.VideoPublicLeft(this);
        //this.addChild(this.firstVideo);
        //video_is_loaded
    }

    //第一个视频加载完毕,开始加载第二个视频

    p.firstVideoLoaded = function(){
        this.leftVideo = new eosi.fotile.VideoPublicLeft(this);
    }
    p.firstVideoclear = function(){
        this.removeChild(this.firstVideo);
        stage.update();
    }
    //扫码页面
    p.setinitwait=function(){
        this.Fotilewait=new eosi.fotile.Fotilewait(this.fotile);
        this.addChild(this.fotilewait);
        container.addChild(this.Fotilewait);
        stage.update();
    }
    //第二个视频等待页面
    p.setupBeforePlayLeftUI = function () {
        this.firstVideoLoaded();
        var master = this;
        var bn = new createjs.Bitmap(master_UILoader.getResult("bn_play"));
        bn.addEventListener("click",clickPlayBn);
        function clickPlayBn(evt){

            //master.playFirstVideo = true;
            if(master.leftVideo.video_is_loaded && master.music_is_loaded){
                master.addLeftVideo()
                master.leftVideo.videoSprite.play();
                createjs.Sound.play("duibai2");
                clearMasterUI();
                bn.removeEventListener("click", clickPlayBn);
            }else{
                console.log("loading is not done!!")
            }
        }
        bn.x = (this.fotile.getWidth()-bn.getBounds().width)/2;
        bn.y = this.fotile.getHeight()/2+150;
        bn.alpha = 0;

        createjs.Tween.get(bn)
            .to({ y: (this.fotile.getHeight()-bn.getBounds().height)/2,alpha:1}, 500, createjs.Ease.sineOut)

        this.addChild(bn);

        //清除masterUI
        function clearMasterUI(){
            createjs.Tween.get(bn)
                .to({ alpha:0}, 500, createjs.Ease.sineOut)

        }
    }

    //将第二个视频添加的舞台上
    p.addLeftVideo = function () {
        this.removeChild(this.firstVideo);
        this.addChild(this.leftVideo);
    }

    p.loadMusic = function () {
        console.log("加载视频配音");
        var mater = this;

        // Create a single item to load.
        var assetsPath = "./_assets/video/";
        //var item = {src:assetsPath+"M-GameBG.ogg", id:"music"};
        var manifest = [
            //{id: "firstduibai", src: assetsPath + "a.ogg"},
            //{id: "firstduibai", src: assetsPath + "M-GameBG.mp3"}
            {id: "firstduibai", src: assetsPath + "a_1.mp3"},
            {id: "duibai2", src: assetsPath + "f1.mp3"}
        ]

        // Instantiate a queue.
        soundLoader = new createjs.LoadQueue();
        //createjs.Sound.registerSound("myAudioPath/mySound.ogg", "myID", 3);
        createjs.Sound.alternateExtensions = ["mp3"];	// add other extensions to try loading if the src file extension is not supported
        soundLoader.installPlugin(createjs.Sound);
        soundLoader.addEventListener("complete", loadComplete);
        soundLoader.addEventListener("fileload", fileComplete);
        soundLoader.addEventListener("error", handleFileError);
        soundLoader.addEventListener("fileprogress", handleFileProgress);
        soundLoader.addEventListener("progress", handleProgress);
        soundLoader.loadManifest(manifest);

        function handleFileError(evt) {
            console.log("error ", evt);
            // An error occurred.
        }
        function handleFileProgress(evt) {
            // Progress happened.
            console.log("Event File Loading: " + (soundLoader.progress.toFixed(2) * 100) + "%");
        }
        function handleProgress(evt) {
            console.log("Event Loading: " + (soundLoader.progress.toFixed(2) * 100) + "%");
            // Progress happened.
        }
        function loadComplete(evt) {
            // Load completed.
            //console.log(evt.target)
            mater.music_is_loaded = true;
            console.log("sounds loaded Complete :)");
            //playSound("firstduibai");

        }
        function fileComplete(evt) {
            //console.log("Event Callback file loaded ", evt);
        }
        function playSound(name) {
            // Play the sound using the ID created above.
            //createjs.Sound.muted = master.fotile.switchSound();
            //createjs.Sound.muted = master.fotile.isMute;
            return createjs.Sound.play(name);
        }


    }






    //漫画 page1
    /*p.page1 = function (){
        var border_thickness = 2;
        var pem = 20;
        var p1_container_mc;
        //p1_container_mc = new createjs.MovieClip(null, 0, true, {start:0});
        p1_container_mc = new createjs.Container();
        this.addChild(p1_container_mc);

        var pop_ss = new createjs.SpriteSheet({
            framerate: 24,
            "images": [animation_Loader.getResult("pop")],
            frames: [
                // x, y, width, height, imageIndex*, regX*, regY*
                [0, 0, 321, 367,0,321,119],
                [1, 1, 321, 367,0,321,119],
                [2, 2, 321, 367,0,321,119],
                [1, 1, 321, 367,0,321,119],
                [319, 2, 210, 399,0,0,270],
                [320, 3, 210, 399,0,0,270],
                [321, 4, 210, 399,0,0,270],
                [320, 3, 210, 399,0,0,270],
                [531, 0, 251, 341,0,125,170],
                [532, 1, 251, 341,0,125,170],
                [533, 2, 251, 341,0,125,170],
                [532, 1, 251, 341,0,125,170],
                //[412, 730, 298, 419,0]
            ],
            "animations": {
                //开始， 结束， 下一个， 速度
                "pop_1": [0,0,"pop_1",.5],
                "pop_2": [4,4,"pop_2",.5],
                "pop_3": [8,11,"pop_3",1]
            }
        });

        var p1_ss = new createjs.SpriteSheet({
            framerate: 24,
            "images": [animation_Loader.getResult("p1")],
            //"images": [queue.getResult("f_0"),queue.getResult("f_1")],
            //frames: {width: 336 , height: 598, regX: 0, regY: 0}

            frames: [
                // x, y, width, height, imageIndex*, regX*, regY*
                [0, 0, 710, 364,0,355,182],
                [0, 381, 710, 330,0],
                [2, 730, 390, 417,0],
                [412, 730, 298, 419,0]
            ],
            "animations": {
                //开始， 结束， 下一个， 速度
                "p1_1_0": [0,0],
                "p1_2_0": [1,1],
                "p1_3_1": [2,2],
                "p1_3_2": [3,3]
            }
        });
        //出图
        var p1_1_0_h = new createjs.Container();
        //var p1_1_0_h = new createjs.MovieClip(null, 0, true, {start:0});
        p1_container_mc.addChild(p1_1_0_h);

        var p1_1_0 = new createjs.Sprite(p1_ss,"p1_1_0");
        p1_1_0.x=p1_1_0.getBounds().width/2;
        p1_1_0.y=p1_1_0.getBounds().height/2;
        createjs.Tween.get(p1_1_0)
            .wait(500)
            .to({ scaleX:1.2,scaleY:1.2}, 3000, createjs.Ease.linear);
        //p1_1_0.scaleX = p1_1_0.scaleY = 10;
        //alert(p1_1_0.scaleX);

        var p1_1_0_b = this.drawPicBoder(p1_1_0.getBounds().width,p1_1_0.getBounds().height,border_thickness);

        var p1_1_0_m = this.drawPicBoder(p1_1_0.getBounds().width,p1_1_0.getBounds().height,border_thickness);
        p1_1_0.mask = p1_1_0_m;
        p1_1_0_h.addChild(p1_1_0_b,p1_1_0);

        var screenRatio = (this.fotile.getWidth()-pem*2)/p1_1_0_h.getBounds().width
        p1_1_0_h.scaleX = p1_1_0_h.scaleY = screenRatio;


        p1_1_0_h.x = pem;//(getWidth() - p1_1_0_h.getBounds().width)/2;
        p1_1_0_h.y = this.fotile.getHeight()/4;
        p1_1_0_h.alpha = 0;
        createjs.Tween.get(p1_1_0_h)
            .to({ alpha:1,y:pem}, 500, createjs.Ease.sineOut);

        //var p1_2_0_h = new createjs.MovieClip(null, 0, true, {start:0});
        var p1_2_0_h = new createjs.Container();
        p1_container_mc.addChild(p1_2_0_h);

        var p1_2_0 = new createjs.Sprite(p1_ss,"p1_2_0");
        var p1_2_0_b = this.drawPicBoder(p1_2_0.getBounds().width,p1_2_0.getBounds().height,border_thickness);
        p1_2_0_h.addChild(p1_2_0_b,p1_2_0);
        p1_2_0_h.scaleX = p1_2_0_h.scaleY = screenRatio;

        p1_2_0_h.x = pem;//(getWidth() - p1_2_0_h.getBounds().width)/2;
        p1_2_0_h.y = this.fotile.getHeight()/2;
        p1_2_0_h.alpha =0;

        createjs.Tween.get(p1_2_0_h)
            .wait(800)
            .to({ alpha:1,y:pem*2 + p1_1_0_h.getBounds().height*screenRatio}, 500, createjs.Ease.sineOut);


        var p1_3_1_h = new createjs.Container();
        //var p1_3_1_h = new createjs.MovieClip(null, 0, true, {start:0});
        p1_container_mc.addChild(p1_3_1_h);

        var p1_3_1 = new createjs.Sprite(p1_ss,"p1_3_1");
        var p1_3_1_b = this.drawPicBoder(p1_3_1.getBounds().width,p1_3_1.getBounds().height,border_thickness);
        p1_3_1_h.addChild(p1_3_1_b,p1_3_1);
        p1_3_1_h.scaleX = p1_3_1_h.scaleY = screenRatio;
        p1_3_1_h.x = -p1_3_1_h.getBounds().width;
        p1_3_1_h.y = pem*3 + p1_1_0_h.getBounds().height*screenRatio + p1_2_0_h.getBounds().height*screenRatio;
        p1_3_1_h.alpha =0;

        createjs.Tween.get(p1_3_1_h)
            .wait(1600)
            .to({ alpha:1,x:pem}, 500, createjs.Ease.sineOut);

        //var p1_3_2_h = new createjs.MovieClip(null, 0, true, {start:0});
        var p1_3_2_h = new createjs.Container();
        p1_container_mc.addChild(p1_3_2_h);

        var p1_3_2 = new createjs.Sprite(p1_ss,"p1_3_2");
        var p1_3_2_b = this.drawPicBoder(p1_3_2.getBounds().width,p1_3_2.getBounds().height,border_thickness);
        p1_3_2_h.addChild(p1_3_2_b,p1_3_2);
        p1_3_2_h.scaleX = p1_3_2_h.scaleY = screenRatio;
        p1_3_2_h.x = this.fotile.getWidth();
        p1_3_2_h.y = pem*3 + p1_1_0_h.getBounds().height*screenRatio + p1_2_0_h.getBounds().height*screenRatio;
        p1_3_2_h.alpha =0 ;
        createjs.Tween.get(p1_3_2_h)
            .wait(1600)
            .to({ alpha:1,x:this.fotile.getWidth()-p1_3_2_h.getBounds().width*screenRatio - pem}, 500, createjs.Ease.sineOut);

        //出对话框1
        var pop_1 = new createjs.Sprite(pop_ss,"pop_2");
        pop_1.scaleX = pop_1.scaleY = 0;
        pop_1.x = p1_1_0_h.getBounds().width*screenRatio/3*2;
        pop_1.y = p1_1_0_h.getBounds().height*screenRatio/7*6 + pem;
        createjs.Tween.get(pop_1)
            .wait(400)
            .to({ scaleX: screenRatio,scaleY:screenRatio}, 800, createjs.Ease.backOut);
        p1_container_mc.addChild(pop_1);

        //出对话框2
        var pop_2 = new createjs.Sprite(pop_ss,"pop_1");
        pop_2.scaleX = pop_2.scaleY = 0;
        pop_2.x = p1_2_0_h.getBounds().width*screenRatio/11*5;
        pop_2.y = this.fotile.getHeight()/8*3;
        createjs.Tween.get(pop_2)
            .wait(1200)
            .to({ scaleX: screenRatio,scaleY:screenRatio}, 500, createjs.Ease.backOut);
        p1_container_mc.addChild(pop_2);

        //出对话框3
        var pop_3 = new createjs.Sprite(pop_ss,"pop_3");
        pop_3.scaleX = pop_3.scaleY = 2;
        pop_3.alpha = 0;
        pop_3.x = this.fotile.getWidth()/5*3;
        pop_3.y = this.fotile.getHeight()/5*4;
        createjs.Tween.get(pop_3)
            .wait(2000)
            .to({ scaleX: screenRatio,scaleY:screenRatio,alpha:1}, 500, createjs.Ease.backOut);
        p1_container_mc.addChild(pop_3);


    }*/

    /*FotileMaster.prototype = {
        fotile:null,
        master_UILoader:null,
        animation_Loader:null,
        animation_is_loaded:false,//动画是否已经加载完毕
        //masterMC:null,//movieClip
        container:null,

        init: function (fotile) {
            this.fotile = fotile;
            container = new createjs.Container();
            this.fotile.container.addChild(container);
            console.log("I'm Master!!!");
            //console.log(this.fotile.container);
            this.loadMasterUIRes(this);
            //console.log(this)
        },
        //加载主机初始化UI资源
        loadMasterUIRes:function (master){
            var manifest = [
                {src:"static/master/UI/bg.jpg",id:"bg"},
                {src:"static/master/UI/play.png",id:"bn_play"},

            ];

            master_UILoader = new createjs.LoadQueue(true);
            master_UILoader.on("error", masterUILoadError);
            master_UILoader.on("complete", masterUILoadComplete);
            master_UILoader.addEventListener("progress", this.fotile.updateLoading);
            master_UILoader.loadManifest(manifest, true, "./_assets/");
            //加载完毕
            function masterUILoadComplete(event){
                //console.log(master)
                //console.log(master.fotile)
                //console.log(this.)
                master.fotile.clearLoading();
                master.setupMasterUI(master);
                master.loadMasterAnimationRes(master);

                //createjs.Ticker.timingMode = createjs.Ticker.RAF;
                //createjs.Ticker.on("tick", tick, this);
            };
            //文件加载失败
            function masterUILoadError(event) {
                console.log("主机UI文件加载失败："+event.target);
            };

        },

        //master UI
        setupMasterUI:function (master){
            //console.log(this.master)

            //alert("aa");
            //console.log(master_UILoader)
            var bg = new createjs.Bitmap(master_UILoader.getResult("bg"));
            //alert(bg.getBounds());
            //alert(getWidth());
            //var ratioX =
            bg.scaleX = this.fotile.getWidth()/bg.getBounds().width;
            bg.scaleY = this.fotile.getHeight()/bg.getBounds().height;
            //bg.setBounds = (0,0,1000,getHeight());
            bg.alpha = 0;

            var bn = new createjs.Bitmap(master_UILoader.getResult("bn_play"));
            //bn.shadow = new createjs.Shadow("#000000", 0, 0, 10);

            bn.addEventListener("click",clickPlayBn);
            function clickPlayBn(evt){
                bn.removeEventListener("click", clickPlayBn);
                //createjs.Tween.get(bn)
                //      .to({ alpha:0}, 200, createjs.Ease.sineOut);
                //alert(evt);
                //loadVideo();
                clearMasterUI();
                if(animation_Loader.loaded){
                    //console.log(master)
                    master.page1();
                }else{
                    alert(animation_Loader.loaded);
                }
                //alert(animation_Loader.loaded);
            }
            bn.x = (this.fotile.getWidth()-bn.getBounds().width)/2;
            bn.y = this.fotile.getHeight()/2+150;
            bn.alpha = 0;

            // set up a tween that tweens between scale 0.3 and 1 every second.
            createjs.Tween.get(bn)
                //.wait(300)
                .to({ y: (this.fotile.getHeight()-bn.getBounds().height)/2,alpha:1}, 500, createjs.Ease.sineOut)

            //console.log(container);
            container.addChild(bg,bn);
            //stage.update();

            //清除masterUI
            function clearMasterUI(){
                createjs.Tween.get(bn)
                    //.wait(500)
                    .to({ alpha:0}, 500, createjs.Ease.sineOut)
                //stage.removeChild(bg,bn);
                createjs.Tween.get(bg)
                    .wait(500)
                    .to({alpha:0}, 500).call(bgOutComplete);
                function bgOutComplete() {
                    //渐变完成执行
                    //loadVideo();

                }
            }

        },


        //加载主机初始化UI资源
        loadMasterAnimationRes:function (master){
            var manifest = [
                {src:"static/master/animation/p1_pic_1.jpg",id:"p1_pic_1"},
                {src:"static/master/animation/p1_pic_2_1.jpg",id:"p1_pic_2_1"},
                {src:"static/master/animation/p1_pic_2_2.jpg",id:"p1_pic_2_2"},
                {src:"static/master/animation/p1_pic_3.jpg",id:"p1_pic_3"},
                {src:"static/master/animation/p1.jpg",id:"p1"},
                {src:"static/master/animation/popup_res.png",id:"pop"},

            ];

            animation_Loader = new createjs.LoadQueue(true);
            //animation_Loader.on("error", animationLoaderError);
            animation_Loader.on("complete", animationLoadComplete);
            animation_Loader.addEventListener("progress", this.fotile.updateLoading);
            animation_Loader.loadManifest(manifest, true, "./_assets/");
            //动画资源加载完毕
            function animationLoadComplete (evt){
                //setupAnimationUI();
                //console.log(master.animation_is_loaded)
                master.animation_is_loaded = true;
                //console.log(master.animation_is_loaded)
            };
        },



        //图片加边框
        drawPicBoder:function (w,h,boder_thick){
            var border = new createjs.Shape();
            border.graphics.setStrokeStyle(boder_thick);
            border.graphics.beginStroke("#000000");
            border.graphics.beginFill("#ad628a").drawRect(0,0, w, h);
            //border.graphics.beginFill("#000000").drawRect(-boder_thick, -boder_thick, w+boder_thick*2, h+boder_thick*2);
            return border;
        },

        //漫画 page1
        page1:function (){
            var border_thickness = 2;
            var pem = 20;
            var p1_container_mc;
            //p1_container_mc = new createjs.MovieClip(null, 0, true, {start:0});
            p1_container_mc = new createjs.Container();
            container.addChild(p1_container_mc);

            var pop_ss = new createjs.SpriteSheet({
                framerate: 24,
                "images": [animation_Loader.getResult("pop")],
                frames: [
                    // x, y, width, height, imageIndex*, regX*, regY*
                    [0, 0, 321, 367,0,321,119],
                    [1, 1, 321, 367,0,321,119],
                    [2, 2, 321, 367,0,321,119],
                    [1, 1, 321, 367,0,321,119],
                    [319, 2, 210, 399,0,0,270],
                    [320, 3, 210, 399,0,0,270],
                    [321, 4, 210, 399,0,0,270],
                    [320, 3, 210, 399,0,0,270],
                    [531, 0, 251, 341,0,125,170],
                    [532, 1, 251, 341,0,125,170],
                    [533, 2, 251, 341,0,125,170],
                    [532, 1, 251, 341,0,125,170],
                    //[412, 730, 298, 419,0]
                ],
                "animations": {
                    //开始， 结束， 下一个， 速度
                    "pop_1": [0,0,"pop_1",.5],
                    "pop_2": [4,4,"pop_2",.5],
                    "pop_3": [8,11,"pop_3",1]
                }
            });

            var p1_ss = new createjs.SpriteSheet({
                framerate: 24,
                "images": [animation_Loader.getResult("p1")],
                //"images": [queue.getResult("f_0"),queue.getResult("f_1")],
                //frames: {width: 336 , height: 598, regX: 0, regY: 0}

                frames: [
                    // x, y, width, height, imageIndex*, regX*, regY*
                    [0, 0, 710, 364,0,355,182],
                    [0, 381, 710, 330,0],
                    [2, 730, 390, 417,0],
                    [412, 730, 298, 419,0]
                ],
                "animations": {
                    //开始， 结束， 下一个， 速度
                    "p1_1_0": [0,0],
                    "p1_2_0": [1,1],
                    "p1_3_1": [2,2],
                    "p1_3_2": [3,3]
                }
            });
            //出图
            var p1_1_0_h = new createjs.Container();
            //var p1_1_0_h = new createjs.MovieClip(null, 0, true, {start:0});
            p1_container_mc.addChild(p1_1_0_h);

            var p1_1_0 = new createjs.Sprite(p1_ss,"p1_1_0");
            p1_1_0.x=p1_1_0.getBounds().width/2;
            p1_1_0.y=p1_1_0.getBounds().height/2;
            createjs.Tween.get(p1_1_0)
                .wait(500)
                .to({ scaleX:1.2,scaleY:1.2}, 3000, createjs.Ease.linear);
            //p1_1_0.scaleX = p1_1_0.scaleY = 10;
            //alert(p1_1_0.scaleX);

            var p1_1_0_b = this.drawPicBoder(p1_1_0.getBounds().width,p1_1_0.getBounds().height,border_thickness);

            var p1_1_0_m = this.drawPicBoder(p1_1_0.getBounds().width,p1_1_0.getBounds().height,border_thickness);
            p1_1_0.mask = p1_1_0_m;
            p1_1_0_h.addChild(p1_1_0_b,p1_1_0);

            var screenRatio = (this.fotile.getWidth()-pem*2)/p1_1_0_h.getBounds().width
            p1_1_0_h.scaleX = p1_1_0_h.scaleY = screenRatio;


            p1_1_0_h.x = pem;//(getWidth() - p1_1_0_h.getBounds().width)/2;
            p1_1_0_h.y = this.fotile.getHeight()/4;
            p1_1_0_h.alpha = 0;
            createjs.Tween.get(p1_1_0_h)
                .to({ alpha:1,y:pem}, 500, createjs.Ease.sineOut);

            //var p1_2_0_h = new createjs.MovieClip(null, 0, true, {start:0});
            var p1_2_0_h = new createjs.Container();
            p1_container_mc.addChild(p1_2_0_h);

            var p1_2_0 = new createjs.Sprite(p1_ss,"p1_2_0");
            var p1_2_0_b = this.drawPicBoder(p1_2_0.getBounds().width,p1_2_0.getBounds().height,border_thickness);
            p1_2_0_h.addChild(p1_2_0_b,p1_2_0);
            p1_2_0_h.scaleX = p1_2_0_h.scaleY = screenRatio;

            p1_2_0_h.x = pem;//(getWidth() - p1_2_0_h.getBounds().width)/2;
            p1_2_0_h.y = this.fotile.getHeight()/2;
            p1_2_0_h.alpha =0;

            createjs.Tween.get(p1_2_0_h)
                .wait(800)
                .to({ alpha:1,y:pem*2 + p1_1_0_h.getBounds().height*screenRatio}, 500, createjs.Ease.sineOut);


            var p1_3_1_h = new createjs.Container();
            //var p1_3_1_h = new createjs.MovieClip(null, 0, true, {start:0});
            p1_container_mc.addChild(p1_3_1_h);

            var p1_3_1 = new createjs.Sprite(p1_ss,"p1_3_1");
            var p1_3_1_b = this.drawPicBoder(p1_3_1.getBounds().width,p1_3_1.getBounds().height,border_thickness);
            p1_3_1_h.addChild(p1_3_1_b,p1_3_1);
            p1_3_1_h.scaleX = p1_3_1_h.scaleY = screenRatio;
            p1_3_1_h.x = -p1_3_1_h.getBounds().width;
            p1_3_1_h.y = pem*3 + p1_1_0_h.getBounds().height*screenRatio + p1_2_0_h.getBounds().height*screenRatio;
            p1_3_1_h.alpha =0;

            createjs.Tween.get(p1_3_1_h)
                .wait(1600)
                .to({ alpha:1,x:pem}, 500, createjs.Ease.sineOut);

            //var p1_3_2_h = new createjs.MovieClip(null, 0, true, {start:0});
            var p1_3_2_h = new createjs.Container();
            p1_container_mc.addChild(p1_3_2_h);

            var p1_3_2 = new createjs.Sprite(p1_ss,"p1_3_2");
            var p1_3_2_b = this.drawPicBoder(p1_3_2.getBounds().width,p1_3_2.getBounds().height,border_thickness);
            p1_3_2_h.addChild(p1_3_2_b,p1_3_2);
            p1_3_2_h.scaleX = p1_3_2_h.scaleY = screenRatio;
            p1_3_2_h.x = this.fotile.getWidth();
            p1_3_2_h.y = pem*3 + p1_1_0_h.getBounds().height*screenRatio + p1_2_0_h.getBounds().height*screenRatio;
            p1_3_2_h.alpha =0 ;
            createjs.Tween.get(p1_3_2_h)
                .wait(1600)
                .to({ alpha:1,x:this.fotile.getWidth()-p1_3_2_h.getBounds().width*screenRatio - pem}, 500, createjs.Ease.sineOut);

            //出对话框1
            var pop_1 = new createjs.Sprite(pop_ss,"pop_2");
            pop_1.scaleX = pop_1.scaleY = 0;
            pop_1.x = p1_1_0_h.getBounds().width*screenRatio/3*2;
            pop_1.y = p1_1_0_h.getBounds().height*screenRatio/7*6 + pem;
            createjs.Tween.get(pop_1)
                .wait(400)
                .to({ scaleX: screenRatio,scaleY:screenRatio}, 800, createjs.Ease.backOut);
            p1_container_mc.addChild(pop_1);

            //出对话框2
            var pop_2 = new createjs.Sprite(pop_ss,"pop_1");
            pop_2.scaleX = pop_2.scaleY = 0;
            pop_2.x = p1_2_0_h.getBounds().width*screenRatio/11*5;
            pop_2.y = this.fotile.getHeight()/8*3;
            createjs.Tween.get(pop_2)
                .wait(1200)
                .to({ scaleX: screenRatio,scaleY:screenRatio}, 500, createjs.Ease.backOut);
            p1_container_mc.addChild(pop_2);

            //出对话框3
            var pop_3 = new createjs.Sprite(pop_ss,"pop_3");
            pop_3.scaleX = pop_3.scaleY = 2;
            pop_3.alpha = 0;
            pop_3.x = this.fotile.getWidth()/5*3;
            pop_3.y = this.fotile.getHeight()/5*4;
            createjs.Tween.get(pop_3)
                .wait(2000)
                .to({ scaleX: screenRatio,scaleY:screenRatio,alpha:1}, 500, createjs.Ease.backOut);
            p1_container_mc.addChild(pop_3);


        }



    }*/
    // add MyApp to myNameSpace
    window.eosi.fotile.FotileMaster = createjs.promote(FotileMaster, "Container");
//}());
}(window));
